package pqt;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import java.awt.Font;

public class Error {

	private JFrame frmGabinetePlastico;
	private Bloque bloque;
	private String item;
	
	List lista = new List();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//Error window = new Error();
					//window.frmGabinetePlastico.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Error(Bloque bloque, String item) {
		
		this.bloque = bloque;
		this.item = item;
		for(int i=0;i<bloque.getFallaSize();i++)
		lista.add(bloque.getFalla(i));
		
		mostrar();
	}
	/**
	 * Create the application.
	 */
	public void mostrar() {
		
		initialize();
		 
		frmGabinetePlastico.getContentPane().setLayout(null);
		
		final JList list = new JList(createData(lista));
		list.setFont(new Font("Tahoma", Font.PLAIN, 20));
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBounds(10, 11,723, 317);
		frmGabinetePlastico.getContentPane().add(scrollPane);
		list.setCellRenderer(new CheckListRenderer());
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setBorder(new EmptyBorder(0, 4, 0, 0));
		
		list.addMouseListener(new MouseAdapter() {
		 public void mouseClicked(MouseEvent e) {
		   int index = list.locationToIndex(e.getPoint());
		   CheckableItem item = (CheckableItem) list.getModel()
		       .getElementAt(index);
		   item.setSelected(!item.isSelected());
		   Rectangle rect = list.getCellBounds(index, index);
		   list.repaint(rect);
		 }
		});
		
		frmGabinetePlastico.getContentPane().add(scrollPane, BorderLayout.CENTER);
		JButton printButton = new JButton("Aceptar");
		printButton.setBounds(181, 424, 173, 41);
		frmGabinetePlastico.getContentPane().add(printButton);
		
		JButton clearButton = new JButton("Cancelar");
		clearButton.setBounds(364, 424, 165, 41);
		frmGabinetePlastico.getContentPane().add(clearButton);
		
		
		clearButton.addActionListener(new ActionListener() {
		 public void actionPerformed(ActionEvent e) {
		  frmGabinetePlastico.dispose();
		 }
		});
		
		printButton.addActionListener(new ActionListener() {
		 public void actionPerformed(ActionEvent e) {
		   
			ListModel model = list.getModel();
		   int n = model.getSize();
		   for (int i = 0; i < n; i++) {
		     CheckableItem item = (CheckableItem) model.getElementAt(i);
		     if (item.isSelected()) {
		    	 
		    	 bloque.item[i].setError(item.toString());
		    	 
		     }
		     frmGabinetePlastico.dispose();
		   }
		 }
		});
		frmGabinetePlastico.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	
	private void initialize() {
		frmGabinetePlastico = new JFrame();
		frmGabinetePlastico.setTitle(item.toUpperCase());
		frmGabinetePlastico.setBounds(100, 100, 759, 527);
		frmGabinetePlastico.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private CheckableItem[] createData(List strs) {
	    int n = strs.countItems();
	    CheckableItem[] items = new CheckableItem[n];
	    for (int i = 0; i < n; i++) {
	      items[i] = new CheckableItem(strs.getItem(i).toString());
	      
	      //los items que no cumplan, los agrego a otra lista, 
	    }
	    return items;
	  }
}
